package com.example.bottommenutest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabHost tabHost;
    private final String[] menus={"首页","消息","我的"};
    private final int[] imgIDs={R.drawable.index,R.drawable.msg,R.drawable.mine};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        viewPager=findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return ChannelFragment.getInstance(menus[position]);
            }

            @Override
            public int getCount() {
                return menus.length;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(tabHost!=null){
                    tabHost.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabHost=findViewById(R.id.tabHost);
        tabHost.setup();
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if(tabHost!=null){
                    int currentTab = tabHost.getCurrentTab();
                    viewPager.setCurrentItem(currentTab);
                }
            }
        });
        for (int i = 0; i < menus.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.channel_menu, null);
            ImageView iv=view.findViewById(R.id.iv);
            TextView tv=view.findViewById(R.id.tv);
            iv.setImageResource(imgIDs[i]);
            tv.setText(menus[i]);

            TabHost.TabSpec tabSpec = tabHost.newTabSpec(menus[i]);
            tabSpec.setIndicator(view);
            tabSpec.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            tabHost.addTab(tabSpec);
        }
    }
}